<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 企业路径管理
 * @package app\Route
 * @author LJQ<245369209@qq.com>
 */
class Route extends Api

{

    /**
   *  企业路径列表
   * @param string enterpriseId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function routeList($enterpriseId = null){
            $where=[];
            if ($enterpriseId) {
              $where['enterpriseId']=['eq',$enterpriseId];
            }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 企业路径列表
			$info =collection(model('Route')->with('enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//企业路径列表
			$info =collection(model('Route')->with('enterprise')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Route')->where($where)->count('id'); 
		
    		if ($info && $count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到企业路径','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'企业路径信息','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
    /**
   *  企业路径列表
   * @param array $data 路径信息
   * @return Json
   */
   	 public function add($data = null){
          
          if (empty($data)||empty($data['enterpriseId'])) {
              return json(['code'=>0,'msg'=>'路径信息错误,请检查']);
          }
          $model = model('Basic/Route');
          $result = $model->change($data);
          if (false != $result) {
             setlog('Routeadd','Route',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'企业路径添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'企业路径添加失败,请检查']);
          }
      }

   	
  /**
   *  删除企业路径
   * @param string $name 路径名称,
   * @param string $enterpriseId 企业ID
   * @return json
   */
     
    public function del($name,$enterpriseId) {
        $model = model('Basic/Route');
      	if (empty($name)||empty($enterpriseId)) {
           	return  json(['code'=>0,'msg'=>'企业路径错误']);
        }
        if($name == '*'){
        	$model->destroy(['enterpriseId'=>$enterpriseId]);
        	return json(['code'=>1,'msg'=>'路径清空成功']);
        }
        if($model->destroy(['routeName'=>$name,'enterpriseId'=>$enterpriseId]) !== FALSE){
            setlog('Routedel','Route',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'企业路径删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'企业路径删除失败,请检查']);
       }
    }
}